package com.example.service.Impl;

import com.example.mapper.StudentMapper;
import com.example.model.Student;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student selectStudent(Integer id) {
        Student student = studentMapper.selectByPrimaryKey(id);
        return student;
    }

    @Override
    public Integer addStudent(Student student) {
        return studentMapper.insertSelective(student);
    }

    @Override
    public Boolean updateStudent(Student student) {
        if (1!=(studentMapper.updateByPrimaryKeySelective(student))){
            return false;
        }else {
            return true;
        }
    }

    @Override
    @Transactional
    public int transactionalStudentById(Student student) {
        int updateCount = studentMapper.updateByPrimaryKeySelective(student);
        System.out.println("更新结果：" + updateCount);
        //在此构造一个除数为 0 的异常，测试事务是否起作用
        int a = 10/0;
        return updateCount;
    }

    @Override
    public Boolean deleteById(Integer id) {
        if (1!=(studentMapper.deleteByPrimaryKey(id))){
            return false;
        }else {
            return true;
        }
    }


}
